﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization.Formatters;
using System.Text;
using System.Threading.Tasks;
using Top.Data;

namespace Top.PointStore.Business
{
    /*功能说明:商品分类操作管理类
    *版本:
    *参与人:王士锋
    *修改日期:2015.3.4
    */
    /// <summary>商品分类
    /// </summary>
    public static class ProductsCategoryManager
    {

        /// <summary>增加一条数据
        /// </summary>
        public static async Task<int> Add(T_ProductsCategory model)
        {
            const string sql = @"insert into T_ProductsCategory ([F_CategoryName],[F_Lever],[F_ParentKid],[F_ParentPath],[F_ProductNum],[F_CreateTime]) 
                values ({0:F_CategoryName},
                        {0:F_Lever},
                        {0:F_ParentKid},
                        {0:F_ParentPath},
                        {0:F_ProductNum},
                        now())
                select LAST_INSERT_ID()";

            using (var db = new DbTour())
            {
                return await db.Sql(sql, model).ExecuteScalar<int>();
            }
        }

        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="kid"></param>
        /// <returns></returns>
        public static async Task<bool> Delete(int kid)
        {
            if (kid <= 0) return false;
            const string sql = @"UPDATE T_ProductsCategory
                SET F_DeletedTime = now(),
                    F_IsDelete = 1
                WHERE kid = {0}";
            using (var db = new DbTour())
            {
                return await db.Sql(sql, kid).ExecuteNonQuery() > 0;
            }
        }

        /// <summary>批量删除
        /// </summary>
        /// <param name="kids"></param>
        /// <returns></returns>
        public static async Task<bool> DeleteList(string kids)
        {
            if (string.IsNullOrEmpty(kids)) return false;
            const string sql = @"UPDATE T_ProductsCategory
                SET F_DeletedTime = now(),
                    F_IsDelete = 1
                WHERE kid in ({0})";
            using (var db = new DbTour())
            {
                return await db.Sql(sql, kids).ExecuteNonQuery() > 0;
            }
        }


        /// <summary>更新一条数据
        /// </summary>
        public static async Task<int> Update(T_ProductsCategory model)
        {
            const string sql = @"update T_ProductsCategory 
                set  F_CategoryName={0:F_CategoryName},
                    F_Lever={0:F_Lever},
                    F_ParentKid={0:F_ParentKid},
                    F_ParentPath={0:F_ParentPath},
                    F_ProductNum={0:F_ProductNum},
                    F_UpdatedTime=now()
                where Kid={0:Kid}";

            using (var db = new DbTour())
            {
                return await db.Sql(sql, model).ExecuteNonQuery();
            }
        }

        /// <summary>根据编号得到一条记录
        /// </summary>
        /// <param name="kid"></param>
        /// <returns></returns>
        public static async Task<T_ProductsCategory> Get(int kid)
        {
            if (kid <= 0) return new T_ProductsCategory();
            const string sql = "select  * from T_ProductsCategory where Kid={0} limit 1";
            using (var db = new DbTour())
            {
                return await db.Sql(sql, kid).FirstOrDefault<T_ProductsCategory>();
            }
        }

        /// <summary>根据条件获取数据列表
        /// </summary>
        /// <returns></returns>
        public static async Task<List<T_ProductsCategory>> GetList(int parentKid)
        {
            using (var db = new DbTour())
            {
                var query = db.Table("select * from T_ProductsCategory");
                if (parentKid >= 0)
                {
                    query.And("F_ParentKid = {0}", parentKid);
                }
                return await query.ToList<T_ProductsCategory>();
            }
        }
    }
}
